package com.heima.behavior.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.behavior.service.ApArticleRelationService;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApLikesBehaviorService;
import com.heima.behavior.service.ApUnlikesBehaviorService;
import com.heima.model.behavior.dtos.ApArticleRelationDto;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.behavior.pojos.ApUnlikesBehavior;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description:
 * @Version: V1.0
 */
@Service
public class ApArticleRelationServiceImpl implements ApArticleRelationService {

    @Autowired
    ApLikesBehaviorService apLikesBehaviorService;
    @Autowired
    ApUnlikesBehaviorService apUnlikesBehaviorService;
    @Autowired
    ApBehaviorEntryService apBehaviorEntryService;

    /**
     * 查询用户文章的点赞\不喜欢
     * @param dto
     * @return
     */
    @Override
    public Map findApArticleRelation(ApArticleRelationDto dto) {
        // 预封装结果
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("islike", false);
        resultMap.put("isunlike", false);
        if (dto==null||dto.getArticleId()==null){
            return resultMap;
        }

        ApBehaviorEntry entry = apBehaviorEntryService.findByUserIdOrEquipmentId(dto.getEntryId(), null);
        if(entry==null){
            return resultMap;
        }
        resultMap.put("entryId", entry.getId());
        //查询相关的数据,如果存在map的值改为true
        ApLikesBehavior apLikesBehavior = apLikesBehaviorService.getOne(Wrappers.<ApLikesBehavior>lambdaQuery()
                .eq(ApLikesBehavior::getArticleId, dto.getArticleId())
                .eq(ApLikesBehavior::getEntryId, entry.getId()));
        if (apLikesBehavior!=null&&apLikesBehavior.getOperation().intValue()==0){
            resultMap.put("islike", true);
        }
        //查询相关的数据,如果存在map的值改为true
        ApUnlikesBehavior apUnlikesBehavior = apUnlikesBehaviorService.getOne(Wrappers.<ApUnlikesBehavior>lambdaQuery()
                .eq(ApUnlikesBehavior::getArticleId, dto.getArticleId())
                .eq(ApUnlikesBehavior::getEntryId, entry.getId()));
        if (apUnlikesBehavior!=null&&apUnlikesBehavior.getType().intValue()==0){
            resultMap.put("isunlike", true);
        }
        return resultMap;
    }
}